/**     
 * @Title: MessageTrunk.java   
 * @Package me.xxy.mq   
 * @Description: TODO
 * @author weiwei 
 * @date 2018年5月30日 下午4:54:01   
 * @version V1.0     
 */
package me.xxy.mq.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

/**
 * @ClassName: MessageTrunk
 * @Description: TODO
 * @author weiwei
 * @date 2018年5月30日 下午4:54:01
 * 
 */
@Service
public class MessageTrunk {

	private static Log LOG = LogFactory.getLog(MessageTrunk.class);

	@Autowired
	private RedisConfig redisConfig;

	@Autowired
	@Qualifier(value = "messageTrunktaskExecutor")
	private ThreadPoolTaskExecutor threadPool;

	/** 失败重试次数，超过此值则不再重试，默认3次 **/
	private int failRetryTimes = 3;

	/** 如果线程池满了，生产者暂停的时间，单位：S **/
	private int threadPoolFullSleepSeconds = 1;

	/**
	 * @return the threadPool
	 */
	public ThreadPoolTaskExecutor getThreadPool() {
		return threadPool;
	}

	/**
	 * @param threadPool
	 *            the threadPool to set
	 */
	public void setThreadPool(ThreadPoolTaskExecutor threadPool) {
		this.threadPool = threadPool;
	}

	/**
	 * @return the failRetryTimes
	 */
	public int getFailRetryTimes() {
		return failRetryTimes;
	}

	/**
	 * @param failRetryTimes
	 *            the failRetryTimes to set
	 */
	public void setFailRetryTimes(int failRetryTimes) {
		this.failRetryTimes = failRetryTimes;
	}

	/**
	 * @return the threadPoolFullSleepSeconds
	 */
	public int getThreadPoolFullSleepSeconds() {
		return threadPoolFullSleepSeconds;
	}

	/**
	 * @param threadPoolFullSleepSeconds
	 *            the threadPoolFullSleepSeconds to set
	 */
	public void setThreadPoolFullSleepSeconds(int threadPoolFullSleepSeconds) {
		this.threadPoolFullSleepSeconds = threadPoolFullSleepSeconds;
	}

	public void put(Message message) {
		LOG.debug("Message >> " + message);
		redisConfig.rpush(message.getKey(), message, null == message.getSeconds() ? 0 : message.getSeconds());
	}
}
